package cn.lf.android.lawfirm.worklog;


import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import cn.lf.android.lawfirm.OrdinaryBaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.changeapproval.controller.ChangeUnderListConroller;
import cn.lf.android.lawfirm.changeapproval.view.ChangeUndertakeAdapter;
import cn.lf.android.lawfirm.distribution.modle.LawCase;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.SPUtil;
import cn.lf.android.lawfirm.worklog.controller.WorkLogListConroller;
import cn.lf.android.lawfirm.worklog.molde.WorkLog;
import cn.lf.android.lawfirm.worklog.view.WorkLogAdapter;

import static cn.lf.android.lawfirm.MyApplication.context;
import static cn.lf.android.lawfirm.R.id.eb_tv_right;
import static cn.lf.android.lawfirm.R.id.img_left;
import static cn.lf.android.lawfirm.R.id.rl_search;

public class WorkLogListActivity extends OrdinaryBaseActivity
        implements WorkLogListConroller.WorkLogListCallback {
    private ListView mListView;
    private SmartRefreshLayout refreshLayout;
    private WorkLogAdapter mWorkLogAdapter;
    private List<WorkLog> list = new ArrayList<WorkLog>();

    private WorkLogListConroller mWorkLogListConroller;
    private int pageIndex = 1;
    /**
     * 分别为：all（全部）、checked（已查阅）、waitCheck（待查阅）、backed（已退回）（必填）
     **/
    private String state = "waitCheck";
    private String flag = "";//日志查阅 传1  工作日志传0
    public static Boolean isRefresh = false;

    @Override
    public int getChildView() {
        return R.layout.activity_undertake_list;
    }

    @Override
    public void initViewId() {
        initTitle();
        flag = getIntent().getStringExtra("flag");
        refreshLayout = (SmartRefreshLayout) findId(R.id.refreshLayout);
        mListView = (ListView) findId(R.id.lv_list);
        mWorkLogAdapter = new WorkLogAdapter(this, list, 0);
        mListView.setAdapter(mWorkLogAdapter);
        tvSearch.setVisibility(View.VISIBLE);
        mSearch.setVisibility(View.GONE);
        setLisenters();
    }

    @Override
    public void refreshLoad() {

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case eb_tv_right:
                Intent intent1 = new Intent(this, AddWorkLogActivity.class);
                startActivity(intent1);
                break;
            case img_left:
                finish();
                break;
            case rl_search:
                Intent intent = new Intent(this, WorkLogSearchListActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }


    private void initTitle() {
        mImgLeft.setVisibility(View.VISIBLE);
        mImgLeft.setOnClickListener(this);
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText(getIntent().getStringExtra("title"));
        tvSearch.setHint("请输入日志主题");
        mTvRight.setText("添加");
        mTvRight.setOnClickListener(this);
        if ("日志查阅".equals(mTitle.getText().toString())) {
            mTvRight.setText("");
        }
    }

    private void setLisenters() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(0);//0秒
                pageIndex = 1;
                list.clear();
                mWorkLogListConroller.onWorkLogList(flag, "", String.valueOf(pageIndex), state);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
                pageIndex++;
                mWorkLogListConroller.onWorkLogList(flag, "", String.valueOf(pageIndex), state);
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent intent = new Intent(ChangeApprovalListActivity.this, CaseDetailActivity.class);
//                intent.putExtra("caseID", list.get(position).caseID);
//                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        getData();
        SPUtil spUtil = new SPUtil(context);
        DebugLog.e("Authorization---" + spUtil.getAccessToken());
        mWorkLogListConroller = new WorkLogListConroller(this, this);
        mWorkLogListConroller.onWorkLogList(flag, "", String.valueOf(pageIndex), state);
    }


    private void getData() {
    }

    @Override
    public void onWorkLogListSuccessed(List<WorkLog> beans) {
        list.addAll(beans);
        mWorkLogAdapter.notifyDataSetChanged();
        if (list.size() == 0) {
            noData2.setVisibility(View.VISIBLE);
        } else {
            noData2.setVisibility(View.GONE);
        }
    }

    @Override
    public void onWorkLogListFailed() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isRefresh) {
            if (null != refreshLayout) {
                refreshLayout.autoRefresh();
                isRefresh = false;
            }
        }
    }
}

